(function($, win, doc){
    var uploadPicObj = null;
    var uploadPic = win.uploadPic = function(obj) {
        uploadPicObj = obj;
        layer.open({
            type: 2,
            title: '上传图片',
            shadeClose: false,
            shade: 0.8,
            maxmin: false, //开启最大化最小化按钮
            area: ['550px', '300px'],
            content: picUploadUrl
        });
    }
    win.setFormResultImg = function (data) {
        layer.closeAll();
        console.log(data);
        if(data.status == 10000) {
            var obj = $(uploadPicObj).prev('.item_url');
            console.log(obj.attr('tagName'));
            obj.prop('src', domainPic + data.result);
            obj.attr('durl', data.result);
        }else {
            layer.alert(data.errmsg);
        }
    }
    win.deleteItem = function (obj) {
        $(obj).parent('.banner_item').remove();
    }
    var addBanner = function() {
        $('#data_box').append($('#item_tpl').html());
    }
    var save = function () {
        var obj = $(this);
        var city = obj.attr('dcity');
        var postUrl = obj.attr('durl');
        var itemUrls = $('#data_box .item_url');
        var itemType = $('#data_box .item_link_type');
        var itemData = $('#data_box .item_link_data');
        var itemSort = $('#data_box .item_link_sort');
        var data = [];
        for(var i=0; i<itemUrls.length; i++) {
            var url = $(itemUrls[i]).attr('durl');
            var link_type = $(itemType[i]).val();
            var link_data = $(itemData[i]).val();
            var link_sort = $(itemSort[i]).val();

            if(!url) {
                layer.alert('【图片】必须上传！');
                return;
            }
            if(!link_type) {
                layer.alert('【点击处理类型】必须选择！');
                return;
            }
            if(link_type != 'none' && !link_data) {
                layer.alert('【点击处理所需数据】必须填写！');
                return;
            }
            if(link_sort != 'none' && !link_sort) {
                layer.alert('【排序ID】必须填写！');
                return;
            }
           
            data.push({url: url, link_type: link_type, link_data: link_data,link_sort:link_sort});          
        }
        layer.load();
        $.post(
            postUrl,
            {data: JSON.stringify(data), city: city},
            function(data){
                if(data.status == 10000) {
                    location.reload(true);
                } else {
                    layer.closeAll();
                    layer.alert(data.errmsg);
                }
            },
            'json'
        );
    }
    $(function(){
        $('#but_add_banner').on('click', addBanner);
        $('#but_save').on('click', save);
    });
})(jQuery, window, document);